package com.demo.web;

import com.demo.bean.Result;
import com.demo.bean.User;
import com.demo.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

/**
 * @Author: maxwellens
 */
@RestController
public class UserController
{
	@Autowired
	private UserService userService;

	@GetMapping("/users/{id}")
	public Result findUserById(@PathVariable Integer id)
	{
		User user = userService.findUserById(id);
		return new Result(user);
	}

	@GetMapping("/users")
	public Result findUsers(Integer page, Integer limit, String name, String gender)
	{
		Map<String, Object> map = new HashMap<>();
		map.put("page", page);
		map.put("limit", limit);
        map.put("name", name);
        map.put("gender", gender);
		return userService.findUsersResult(map);
	}

	@DeleteMapping("/users/{id}")
	public Result deleteUserById(@PathVariable Integer id)
	{
		userService.deleteUserById(id);
		return Result.SUCCESS;
	}

	@PutMapping("/users")
	public Result saveUser(User user)
	{
		userService.saveUser(user);
		return Result.SUCCESS;
	}

}
